# -*- coding:utf-8 -*-
from flask import request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao_v2.userRechargeDao import UserRechargeDao
import time
from . import app

localTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))


CORS(app, supports_credentials=True)

@app.route('/queryUserRechargeById', methods=['POST'])
def queryUserRechargeById():

    if not request.json:
        abort(400)
    id = request.json.get("id")
    try:
        userRechargeJSON,TC= UserRechargeDao().queryUserRechargeById(id)#TC   等价于 totalCount
        if TC:
            return jsonify({"message": {"code": 1, "status": "queryuserRecharge OK"},"info":userRechargeJSON})
        else:
            return jsonify({"info": [], "message": {"code": 0, "status": "No data"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})


@app.route('/queryUserRechargeByCondition', methods=['POST'])
def queryUserRechargeByCondition():

    if not request.json:
        abort(400)
    try:
        page = request.json['page']
        pageIndex = page["pageIndex"]
        pageSize = page["pageSize"]
    except:
        return jsonify({"message": {"code": 0, "status": "请传入 pageIndex和pageSize"}})
    try:
        conditions = request.json['condition']
    except:
        return jsonify({"message": {"code": 0, "status": "未传入指定condition"}})
    try:
        multiSort = request.json['multiSort']
    except:
        return jsonify({"message": "未传入排序规则multiSort"})
    try:
        total_count, rs = UserRechargeDao().queryUserRechargeByCondition(pageIndex=pageIndex, pageSize=pageSize, conditions=conditions, multiSort=multiSort)
        if total_count:
                return jsonify({"message": {"code": 1, "status": "queryUserRecharge OK"}, "totalCount": total_count, "info": rs})
        else:
            return jsonify({"info": [], "message": {"code": 0, "status": "No data"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})



@app.route('/insertUserRecharge', methods=['POST'])
def insertUserRecharge():

    if not request.json:
        abort(400)

    Info = {
        'userId': request.json.get('userId'),  # 必填，请传递userd
        'userName': request.json.get('userName'),  #
        'rechargeNumber': request.json.get('rechargeNumber'),  #
        'paymentId': request.json.get('paymentId'),  #
        'amount': request.json.get('amount'),  #
        'status': request.json.get('status', 0),
        'completeTime':request.json.get('completeTime',localTime)
    }
    if not Info:
        return jsonify({"message": {"code": 0, "info": "信息填写未完整"}})
    try:
        rs = UserRechargeDao().addUserRecharge(Info)
        if rs:
            return jsonify(
                {"massage": {"code": 1, "info": "addUserRecharge OK"}})  # 添加函数调用成功，返回添加的信息（不包括userRecharge的Id）
        else:
            return jsonify({"message": {"code": 0, "info": "addUserRecharge Error"}})  # 添加函数调用失败
    except Exception, e:
            return jsonify({"message": {"code": 0, "info": str(e)}})


@app.route('/updateUserRecharge', methods=['POST'])
def updateUserRecharge():

    if not request.json:
        abort(400)
    try:
        id = request.json['id']
    except:
        return jsonify({"message": {"code": 0, "status": "请传入 id"}})
    oldInfo = UserRechargeDao().selectUserRecharge(id)

    info = {
        'id':request.json['id'],
        'userId': request.json.get('userId', oldInfo[1]),  # 必填，请传递userd
        'userName': request.json.get('userName',oldInfo[2]),  #
        'rechargeNumber': request.json.get('rechargeNumber',oldInfo[3]),  #
        'paymentId': request.json.get('paymentId',oldInfo[4]),  #
        'amount': request.json.get('amount', oldInfo[5]),  #
        'status': request.json.get('status', oldInfo[6]),
        'completeTime':request.json.get('completeTime',oldInfo[8])


    }
    try:
        info = UserRechargeDao().updateUserRecharge(info)

        if info:
            return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
        else:
            return jsonify({"message": {"code": 0, "status": "error"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})






@app.route('/deleteUserRecharge', methods=['POST'])  # 根据id数组删除zzh_user_recharge表指定记录,传入格式为 "ids":[1,2,3,4]
def deleteUserRecharge():
    ids = request.json.get("ids")
    if not ids:
        return jsonify({"message":"请输入待删除的userRecharge id数组！"})
    try:
        rs = UserRechargeDao().deleteUserRecharge(ids)
        if rs:
            return jsonify({"message": {"code": 1, "info": "DeleteUserRecharge OK"}})
        else:
            return jsonify({"message": {"code": 0, "info": "DeleteUserRecharge Error"}})

    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})

